<?php

namespace XWAM\TagLib;

/**
 * website_menu 类
 *
 * @since VER:1.0; DATE:2016-8-24; AUTHOR:SoChishun; EMAIL:14507247@qq.com; DESC:Added.
 */
class website_menu extends TagBase {

    protected $tags = array(
        'get_list' => array('attr' => 'attrs', 'level' => 1),
        'get_tree' => array('attr' => 'attrs', 'level' => 3),
    );

    /**
     * foreach标签解析 循环输出数据集
     * @access public
     * @param array $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     * @example path description
     * @since 1.0 <2015-10-27> SoChishun Added.
     * @example 
     * <website_menu:get_list code="head_menu">
     *      <li><a href="{:U('AboutUs/index','id='.$vo['id'])}">{$vo.title}</a></li>
     * </website_menu:get_list>
     */
    public function _get_list($attr, $content) {
        $options = $this->parseSQLOption($attr);
        if (!empty($attr['code'])) {
            $pid = M('t_porg_website_menu')->where(array('code' => $attr['code']))->getField('id');
            if (!$pid) {
                return false;
            }
            $options['where']['pid'] = $pid;
        }
        $options = array_merge($options, array('sort' => 'sort, id'));
        $list=M('t_porg_website_menu')->select($options);
        $name = '$' . (empty($attr['name']) ? 'res_list' : $attr['name']);
        $item = empty($attr['item']) ? 'vo' : $attr['item'];
        $key = !empty($attr['key']) ? $attr['key'] : 'key';
        $parseStr = '<?php ' . $name . '=M(\'t_porg_website_menu\')->select(' . var_export($options, true) . '); ?>';
        $parseStr .= '<?php if(is_array(' . $name . ')): foreach(' . $name . ' as $' . $key . '=>$' . $item . '): ?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php endforeach; endif; ?>';
        return $parseStr;
    }

    /**
     * foreach标签解析 循环输出数据集
     * @access public
     * @param array $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     * @example path description
     * @since 1.0 <2015-10-27> SoChishun Added.
     * @example 
     * 
      <website_menu:get_tree where='{"pid":36,"status":1}' order="sort,id desc" field="id, title, type_id">
      <li>
      <if condition="1 eq $vo['type_id']">
      <a href="{:U('Services/services_info','id='.$vo['id'])}">{$vo.title}</a>
      <else/>
      <a href="{:U('Services/index','id='.$vo['id'])}">{$vo.title}</a>
      </if>
      </li>
      </website_menu:get_tree>
     */
    public function _get_tree($attr, $content) {
        $options = $this->parseSQLOption($attr);
        // where内容只能是json_encode()处理过的字符串,如where='{"pid":20}'
        if (!empty($options['where']) && !is_array($options['where'])) {
            throw new Exception('[TREE]的where条件格式无效!');
        }
        if (!empty($attr['code'])) {
            $pid = M('t_porg_website_menu')->where(array('code' => $attr['code']))->getField('id');
            if (!$pid) {
                return false;
            }
            $options['where']['pid'] = $pid;
        }
        $name = '$' . (empty($attr['name']) ? 'res_tree' : $attr['name']);
        $item = empty($attr['item']) ? 'vo' : $attr['item'];
        $key = !empty($attr['key']) ? $attr['key'] : 'key';
        $parseStr = '<?php ' . $name . '=D(\'XWAM/WebsiteMenu\')->select_tree(' . var_export($options, true) . '); ?>';
        $parseStr .= '<?php if(is_array(' . $name . ')): foreach(' . $name . ' as $' . $key . '=>$' . $item . '): ?>';
        $parseStr .= $this->tpl->parse($content);
        $parseStr .= '<?php endforeach; endif; ?>';
        return $parseStr;
    }

}
